/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 * 作者:李蕊
 */

import React, {FC, useMemo} from 'react';
import {ProColumns} from "@ant-design/pro-table/lib/Table";
import service from './service';
import {SysErrorLogModel} from './data';
import Details from './components/Details';
import ProTableCrud, {ProTableCrudOperate} from '@/components/ProTableCrud';
import useList, {ListOperate} from '@/hooks/useList';
import moment from "moment";
import {formatDate} from "@/utils/utils";
import useDialog from "@/hooks/useDialog";
import {DialogOperate} from "@/global";

const startDate = moment().subtract(7, 'd');
const endDate = moment();


/** 错误日志模块 */
const SysErrorLogPage: FC = () => {


  const detailsOperate = useDialog();
  //List
  const listOperate = useList<SysErrorLogModel>({
    onLoad: (params) => {
      const {operateDateTime, ...rest} = params;
      const operateStartDateTime = (operateDateTime && operateDateTime[0]) || (formatDate(startDate));
      const operateEndDateTime = (operateDateTime && operateDateTime[1]) || (formatDate(endDate));
      return service.selectPageList({...rest, operateStartDateTime, operateEndDateTime});
    },
  });

  //Details
  // const detailsOperate = useDetails<SysErrorLogModel>({
  //   onDetails: (r) => service.selectById(r.id),
  // });

  //表格列
  //const cols = useMemo(() => getCols(listOperate, detailsOperate), []);
  const cols = useMemo(() => getCols(listOperate, detailsOperate), []);

  //增删改查选项
  const tableOperate: ProTableCrudOperate<SysErrorLogModel> = {
    showToolbar: false,
    showOptionColumn: false,
    cols,
    listOperate,
    detailsOperate,
    getDetailsElement: (props) => (<Details {...props}/>),
  };

  return (
    <ProTableCrud rowSelection={undefined} operate={tableOperate}/>
  );

};

/** 获取表格列 */
function getCols(listOperate: ListOperate<SysErrorLogModel>, detailsOperate: DialogOperate): (ProColumns<SysErrorLogModel>[]) {
  return [
    {
      title: '操作日期',
      dataIndex: 'operateDateTime',
      align: 'center',
      sorter: true,
      width: 170,
      order: 5,
      valueType: 'dateRange',
      initialValue: [startDate, endDate],
      render: (text, r) => (<a onClick={() => detailsOperate.show({id: r.id})}>{text}</a>),
      formItemProps: {
        'placeholder': ['开始日期', '结束日期'],
        'disabledDate': (current: any) => {
          //不能选择大于今天的日期
          return current && current > moment();
        },
      },
    },
    {
      title: '操作人',
      dataIndex: 'operateUserName',
      hideInTable: false,
      hideInSearch: false,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      width: 150,
      align: 'center',
      formItemProps: {
        'placeholder': '请输入操作人姓名',
      },
    },
    {
      title: '主键',
      dataIndex: 'id',
      hideInTable: true,
      hideInSearch: true,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      formItemProps: {
        'placeholder': '请输入主键',
      },
    },
    {
      title: '请求地址',
      dataIndex: 'reqUrl',
      hideInTable: false,
      hideInSearch: false,
      ellipsis: true,
      sorter: false,
      align: 'left',
      width: 300,
      formItemProps: {
        'placeholder': '请输入请求地址',
      },

    },
    {
      title: '请求方法',
      dataIndex: 'reqMethod',
      hideInTable: false,
      hideInSearch: true,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      width: 140,
      align: 'center',
      formItemProps: {
        'placeholder': '请输入请求方法',
      },
    },
    {
      title: '请求参数',
      dataIndex: 'reqParam',
      hideInTable: true,
      hideInSearch: true,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      formItemProps: {
        'placeholder': '请输入请求参数',
      },
    },
    {
      title: '错误消息',
      dataIndex: 'msg',
      hideInTable: true,
      hideInSearch: true,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      formItemProps: {
        'placeholder': '请输入错误消息',
      },
    },
    {
      title: '错误详细信息',
      dataIndex: 'details',
      hideInTable: true,
      hideInSearch: true,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      formItemProps: {
        'placeholder': '请输入错误详细信息',
      },
    },
    {
      title: '应用主键',
      dataIndex: 'appId',
      hideInTable: false,
      hideInSearch: true,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      width: 170,
      formItemProps: {
        'placeholder': '请输入应用主键',
      },
    },
    {
      title: '应用名称',
      dataIndex: 'appName',
      hideInTable: false,
      hideInSearch: false,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      width: 170,
      formItemProps: {
        'placeholder': '请输入应用名称',
      },
    },
    {
      title: 'IP地址',
      dataIndex: 'ip',
      hideInTable: false,
      hideInSearch: false,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      width: 170,
      formItemProps: {
        'placeholder': '请输入IP地址',
      },
    },
    {
      title: '操作人主键',
      dataIndex: 'operateUserId',
      hideInTable: true,
      hideInSearch: true,
      ellipsis: false,
      sorter: false,
      filters: undefined,
      align: 'center',
      formItemProps: {
        'placeholder': '请输入操作人主键',
      },
    },
    {
      key: 'other',
    },

  ]
}

export default SysErrorLogPage;

